package com.micro.frog.acct.rest;

import com.micro.frog.acct.entity.Dict;
import com.micro.frog.acct.service.DictService;
import com.micro.frog.acct.vo.DictVO;
import com.micro.frog.core.common.PageResult;
import com.micro.frog.core.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author :  ljh
 * @date : 2018/4/11
 **/
@Slf4j
@RestController
@RequestMapping("/rbac/dict")
public class DictController {


    @Autowired
    DictService dictService;

    /**
     * 带条件查询带分页
     */
    @GetMapping(value = "/list")
    public PageResult listPage(DictVO dictVO) {
        PageResult result = new PageResult();
        try {
            result = dictService.listPage(dictVO);
        } catch (Exception e) {
            result.setCode("20000");
            result.setMsg("系统异常,{}" + e.getMessage());
            log.error("系统异常 e={}", e);
        }
        return result;
    }

    /**
     * 查询全部不带分页
     *
     * @return
     */
    @GetMapping(value = "/all")
    public Result nopage(DictVO dictVO) {
        Result result = new Result();
        try {
            Dict dict = new Dict();
            BeanUtils.copyProperties(dictVO, dict);
            result.setFlag(true);
            result.setData(dictService.all(dict));
        } catch (Exception e) {
            result.setFlag(false);
            result.setMsg("系统异常");
            log.error("系统异常 e={}", e);
        }
        return result;
    }

    @PostMapping("/add")
    public Result add(Dict dict) {
        Result result = new Result();
        try {
            dictService.add(dict);
            result.setFlag(true);
            result.setMsg("新增成功");
        } catch (Exception e) {
            result.setFlag(false);
            result.setMsg("新增失败");
            log.error("新增失败 e={}", e);
        }
        return result;
    }

    @PostMapping("/delete")
    public Result deleteDict(Integer id) {
        Result result = new Result();
        try {
            dictService.delete(id);
            result.setFlag(true);
            result.setMsg("删除成功");
        } catch (Exception e) {
            result.setFlag(false);
            result.setMsg("系统异常");
            log.error("系统异常 e={}", e);
        }
        return result;
    }

    @PostMapping("/update")
    public Result update(Dict dict) {
        Result result = new Result();
        try {
            dictService.update(dict);
            result.setFlag(true);
            result.setMsg("更新成功");
        } catch (Exception e) {
            result.setFlag(false);
            result.setMsg("系统异常");
            log.error("系统异常 e={}", e);
        }
        return result;
    }


}
